<?

class Selector {
/*----------------------------------------------------------------------------------------------*/

function Selector($anyDB, $tables, $map, $delimiters, $additional_map = array()){
	if(!is_object($anyDB) or !is_string($tables) or empty($tables) or !is_array($map) or empty($map) or !is_array($delimiters) or empty($delimiters) or !is_array($additional_map)){
		return false;
	}
	$this->db = $anyDB;
	$this->tables = $tables;
	$this->map = $map;
	$this->delimiters = $delimiters;
	$this->additional_map = $additional_map;
}
/*----------------------------------------------------------------------------------------------*/

function get_data($params, $option, $return_type){
        if($options['group_by']){
        	$tmp[] = 'count(*)';
        	foreach($options['group_by'] as $k=>$v){
        		$tmp[] = $this->map[$v].' as '.$v;
        	}
                $select_string = implode(',',$tmp);
                $group_by = ' GROUP BY '.implode(',',$options['group_by']);
                if($options['order_by'] and $options['order_by_']){
                        $order_by = 'ORDER BY '.$this->map[($options['order_by'])].' '.$options['order_by_'];
                }
        }else{
        	foreach($this->map as $k=>$v){
        		$tmp[] = $v.' as '.$k;
        	}
                $select_string = implode(',',$tmp);
                $group_by = '';
                if($options['order_by'] and $options['order_by_']){
                        $order_by = 'ORDER BY '.$this->map[($options['order_by'])].' '.$options['order_by_'];
                }
        }

        if(!isset($options['limit']) or !isset($options['offset'])){
                $limit_offset = 'LIMIT 50 OFFSET 0';
        }else{
                $limit_offset = ' LIMIT '.$options['limit'].' OFFSET '.$options['offset'];
        }

	$where = build_where($params, $this->map, $this->additional_map);

        $query = "SELECT $select_string FROM $tables $where $group_by $order_by $limit_offset";
	$query1 = "SELECT $select_string FROM $tables $where $group_by $order_by";

	if($return_type=='query'){
		return $query;
	}

	$this->anyDB->query($query1);
        if($this->anyDB->error){
                return false;
        }

	$res['num_rows'] = $local_connect->numRows();
	$this->anyDB->free();

	$local_connect->query($query);
	if($local_connect->error){
		return false;
	}
	
	$res['result'] = $local_connect->getAll();
	$local_connect->free();

	return $res;
}
/*----------------------------------------------------------------------------------------------*/

function parse_where_value($value){
        if(!is_array($value)){
                if(substr($value,0,1)=='%' and substr($value,strlen($value)-1,1)=='%'){
                        return " LIKE '$value' ";
                }elseif(substr($value,0,1)=='@'){
                        return '='.substr($value,1,strlen($value)-1);
                }elseif(substr($value,0,1)=='|'){
                        return substr($value,1,2)."'".substr($value,3,strlen($value)-1)."'";
                }else{
                        return "='$value'";
                }
        }else{
                foreach($value as $k=>$val){
                        $ret[] = parse_where_value($val);
                }
                return $ret;
        }
}
/*----------------------------------------------------------------------------------------------*/

function build_where($params_array, $map, $additional_map=array()){
	foreach($params_array as $k=>$v){
		if(in_array($k,array_keys($map)) and trim($v)){
			$tmp = parse_where_value($v);
			if(is_array($tmp)){
				$where[] = '('.$map[$k].implode(' OR '.$map[$k],$tmp).')';
			}else{
				$where[] = $map[$k].$tmp;
			}
		}elseif(in_array($k,array_keys($additional_map)) and trim($v)){
			$tmp = parse_where_value($v);
			if(is_array($tmp)){
				$tmp2 = array();
				foreach($additional_map[$k] as $kk=>$vv){
					foreach($tmp as $kkk=>$vvv){
						$tmp2[] = $map[$vv].$vvv;
					}
				}
				$where[] = '('.implode(' OR ', $tmp2).')';
			}else{
				$tmp2 = array();
				foreach($additional_map[$k] as $kk=>$vv){
					$tmp2[] = $map[$vv].$tmp;
				}
				$where[] = '('.implode(' OR ', $tmp2).')';
			}
		}
	}

	if($where){
		return ' WHERE '.implode(' AND ',$where);
	}else{
		return '';
	}
}
/*----------------------------------------------------------------------------------------------*/

function add_delimiters($map, $arr){
        $res = $arr;
        foreach($arr as $k=>$v){
                if(!is_array($v) and trim($v)){
                        if($map[$k]=='%'){
                                $res[$k] = $map[$k].$v.$map[$k];
                        }elseif($map[$k]=='@' or substr($map[$k],0,1)=='|'){
                                $res[$k] = $map[$k].$v;
                        }
                }else{
                        $tmp_map = array();
                        foreach($arr[$k] as $kk=>$vv){
                                $tmp_map[$kk] = $map[$k];
                        }
                        $res[$k] = add_delimiters($tmp_map,$arr[$k]);
                }
        }

        return $res;
}
/*----------------------------------------------------------------------------------------------*/

}
?>